package org.abettor.pushbox.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.abettor.pushbox.model.NotifyConfigBean;
import org.abettor.pushbox.model.SelfMessage;

import android.content.Context;
import android.os.Environment;

/**
 * 各种配置信息
 * @author jzh
 *
 */
public class Config {
	public static final String website="http://www.nook-info.com/pushbox/";
	public static final String checkNickUrl = website + "queryNickName.action";//检查昵称是否存在的URL
	public static final String uploadNickUrl = website + "registerNickName.action";//增加昵称的操作
	public static final String loginNickUrl = website + "loginNickName.action";//登录的操作
	public static final String uploadFileUrl = website + "uploadMapFile.action";//上传文件的url
	public static final String downloadMapFileUrl = website + "downloadMapFile.action";//下载文件的url
	public static final String listMapFileUrl = website + "listMapFile.action";//列出地图列表的url
	public static final String myNeedAnswerMapUrl = website + "myNeedAnswerMap.action";//
	public static final String uploadAnswerUrl = website + "uploadAnswer.action";//上传地图答案的操作
	public static final String listAnswerUrl = website + "listAnswer.action";//获取答案列表的操作
	public static final String downloadAnswerUrl = website + "downloadAnswer.action";//下载答案的操作
	public static final String myResolvedMapUrl = website + "myResolvedMap.action";//获取我解决的地图列表的操作
	public static final String markIResolved = website + "markIResolved.action";//标记地图我已解决的操作
	public static final String modifyPassword = website + "modifyPassword.action";//进行修改密码的操作
	public static final String needAnswerMap = website + "needAnswerMap.action";//列出需要求解地图的列表
	public static final String uploadAnswer = website + "uploadAnswer.action";//上传答案的操作
	public static final String downloadAnswer = website + "downloadAnswer.action";//列出答案列表的操作
	public static final String reply = website + "reply.action";//进行反馈意见的操作
	public static final String queryUserInfo = website + "queryUserInfo.action";//列出用户的信息
	public static final String getNetMapDetail = website + "getNetMapDetail.action";//获得网络地图的详情
	public static final String markFlower = website + "markFlower.action";//送鲜花的操作
	public static final String markEgg = website + "markEgg.action";//扔鸡蛋的操作
	public static final String listCommentEarlyUrl = website + "commentsEarly.action";//列出早期评论的列表
	public static final String listCommentNewestUrl = website + "commentsNewest.action";//列出最近评论的列表
	public static final String addCommentUrl = website + "addComment.action";//加载评论的操作
	public static final String listResolvedUserUrl = website + "listResolvedUser.action";//列出解决用户的列表
	public static final String listMessageBoxUrl = website + "listMsgBox.action";//列出消息的列表
	public static final String listEarlyMessageUrl = website + "listEarlyMsg.action";//列出消息的列表
	public static final String listNewMessageUrl = website + "listNewMsg.action";//列出较晚消息的列表
	public static final String listNotifyUrl = website + "listNotice.action";//列出通知的url
	public static final String addMsgUrl = website + "addMsg.action";//加载评论的操作
	public static final String folder = ".ilovepushbox";
	public static final String classicfolder = "classic";
	public static final String selffolder = "self";
	public static final String netmapfolder = "net";
	public static final int httpTimeOut = 10*1000;//http链接的超时时间
	public static final int httpSoTimeOut = 10*1000;//http so超时时间
	public static final int httpBufferSize = 8192;//http bufersize
	public static final int WATCH_ALARM_TIME_OUT = 1000*10;
	public static final int PAGESIZE = 10;
	/**
	 * 获得文件夹的基准路径
	 * @return
	 * @throws IOException 
	 */
	public static String getFolderBasePath(){
		try{
			File file = new File(Environment.getExternalStorageDirectory()+File.separator+folder);
			if(!file.exists()){
				file.mkdir();
			}
			return file.getPath();
		}catch(Exception e){
			return null;
		}
	}
	/**
	 * 获得网络地图的基准路径
	 * @return
	 */
	public static String getNetBasePath(){
		try{
			String basePath = getFolderBasePath();
			File file = new File(basePath + File.separator+netmapfolder);
			if(!file.exists()){
				file.mkdir();
			}
			return file.getPath();
		}catch(Exception e){
			return null;
		}
	}
	/**
	 * 获得经典map的基准路径
	 * @return
	 */
	public static String getClassicBasePath(){
		try{
			String basePath = getFolderBasePath();
			File file = new File(basePath + File.separator+classicfolder);
			if(!file.exists()){
				file.mkdir();
			}
			return file.getPath();
		}catch(Exception e){
			return null;
		}
	}
	/**
	 * 获得自制图库的路径
	 * @return
	 */
	public static String getSelfBasePath(){
		try{
			String basePath = getFolderBasePath();
			File file = new File(basePath + File.separator+selffolder);
			if(!file.exists()){
				file.mkdir();
			}
			return file.getPath();
		}catch(Exception e){
			return null;
		}
	}
	private static final String PUSH_BOX_CONFIG_FILE_NAME="push_box_config_file";
	private static final String HAS_INIT_FILE_NAME="has_inited_file";
	private static final String SELF_MESSAGE_CONFIG="self_message_config";
	private static final String NOTIFY_CONFIG = "notify_config";
	/**
	 * 是否已经初始化完毕
	 * @return
	 */
	public static boolean isInited(Context context){
		Properties p = readFile(context,HAS_INIT_FILE_NAME);
		if(p.getProperty("hasread")==null){
			return false;
		}else{
			return true;
		}
	}
	/**
	 * 保存为初始化完毕
	 * @param context
	 */
	public static void saveInited(Context context){
		Properties p = new Properties();
		p.setProperty("hasread", "1");
		writeFile(context,HAS_INIT_FILE_NAME,p);
	}
	/**
	 * 读取更新的bean
	 * @param context
	 * @return
	 */
	public static NotifyConfigBean readNotifyConfigBean(Context context){
		NotifyConfigBean bean = new NotifyConfigBean();
		Properties p = readFile(context,NOTIFY_CONFIG);
		String sleepTimeStr = p.getProperty("sleepTime");
		if(sleepTimeStr!=null){
			bean.setSleepTime(Long.parseLong(sleepTimeStr));
		}
		String isOpenNotify = p.getProperty("isOpenNotify");
		if(isOpenNotify != null){
			bean.setIsOpenNotify(isOpenNotify);
		}
		String lastAccessTime = p.getProperty("lastAccessTime");
		if(lastAccessTime != null){
			bean.setLastAccessTime(lastAccessTime);
		}else{
			bean.setLastAccessTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		}
		return bean;
	}
	/**
	 * 读取个人配置的信息
	 * @return
	 */
	public static SelfMessage readSelfMessage(Context context){
		Properties p = readFile(context,SELF_MESSAGE_CONFIG);
		String nick = p.getProperty("nickName");
		if(nick==null){
			return null;
		}
		SelfMessage self = new SelfMessage();
		self.setNickName(nick);
		return self;
	}
	/**
	 * 把文件写入配置文件
	 * @param context
	 * @param self
	 */
	public static void writeSelfMessage(Context context,SelfMessage self){
		Properties p = new Properties();
		p.setProperty("nickName", self.getNickName());
		writeFile(context,SELF_MESSAGE_CONFIG,p);
	}
	/**
	 * 把配置信息写入配置文件
	 * @param context
	 */
	public static void writeNotifyConfigBean(Context context,NotifyConfigBean bean){
		Properties p = new Properties();
		p.setProperty("isOpenNotify", bean.getIsOpenNotify());
		p.setProperty("sleepTime", String.valueOf(bean.getSleepTime()));
		p.setProperty("lastAccessTime", bean.getLastAccessTime());
		writeFile(context,NOTIFY_CONFIG,p);
	}
	//写入文件的操作
	private static void writeFile(Context context,String fileName,Properties p){
		FileOutputStream fos = null;
		try {
			fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
			p.save(fos, "");
			fos.close();
		} catch (IOException e) {
		}finally{
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
				}
			}
		}

	}
	//读取文件的操作
	private static Properties readFile(Context context,String fileName){
		FileInputStream fin = null;
		Properties p = new Properties();
		try {
			fin = context.openFileInput(fileName);
			p.load(fin);
		} catch (IOException e) {
		}finally{
			if(fin != null){
				try {
					fin.close();
				} catch (IOException e) {
				}
			}
		}
		return p;
	}
	/**
	 * 读取配置文件
	 * @param context
	 * @return
	 */
	public static PushBoxConfig readPushBoxConfig(Context context){
		Properties p = readFile(context,PUSH_BOX_CONFIG_FILE_NAME);
		PushBoxConfig config = new PushBoxConfig();
		String shakeOn = p.getProperty("shakeOn");
		String fullScreenOn = p.getProperty("fullScreenOn");
		if(shakeOn!=null){
			config.setShakeOn(Integer.parseInt(shakeOn));
		}else{
			config.setShakeOn(1);
		}
		if(fullScreenOn!=null){
			config.setFullScreenOn(Integer.parseInt(fullScreenOn));
		}else{
			config.setFullScreenOn(0);
		}
		return config;
	}
	/**
	 * 保存PushBoxConfig
	 * @param context
	 * @param config
	 */
	public static void savePushBoxConfig(Context context,PushBoxConfig config){
		Properties p = new Properties();
		p.setProperty("fullScreenOn", String.valueOf(config.getFullScreenOn()));
		p.setProperty("shakeOn", String.valueOf(config.getShakeOn()));
		writeFile(context,PUSH_BOX_CONFIG_FILE_NAME,p);
	}
	//推箱子的各种配置
	public static class PushBoxConfig{
		private int shakeOn = 0;//震动是否开
		private int fullScreenOn = 0;//是否全屏
		public int getShakeOn() {
			return shakeOn;
		}
		public void setShakeOn(int shakeOn) {
			this.shakeOn = shakeOn;
		}
		public int getFullScreenOn() {
			return fullScreenOn;
		}
		public void setFullScreenOn(int fullScreenOn) {
			this.fullScreenOn = fullScreenOn;
		}
	}
	
}
